<?php

class CostsController extends Controller {

    public function actionIndex() {
        $promoCodeID = isset($_GET['promoCode']) ? (int) $_GET['promoCode'] : Promocodes::model()->find('1=1 order by id')->id;
        $group = isset($_GET['group']) ? $_GET['group'] : 'd';
        $models = array();
        $logs = array();
        $sql = 'SELECT
                            s.evstat_payable,
                            l.status_log_begin,
                            l.status_log_end,
                            u.user_fname,
                            c.city_price
                    FROM
                            ev_users u
                    JOIN ev_evaks e ON u.user_id = e.evak_user :: INT
                    JOIN ev_evak_log l ON e.evak_id = l.evak_id :: INT
                    JOIN ev_evstats s ON l.evak_status_id = s.evstat_id
                    JOIN ev_cities c ON e.evak_city = c.city_id
                    WHERE
                            s.evstat_payable = 1
                    AND u.promo_code :: INT = ' . $promoCodeID . ' ORDER BY l.status_log_begin DESC';
        $models = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
        foreach ($models as $model) {
            $logs[date('d.m.Y', $model['status_log_begin'])][] = $model;
        }
        
//        echo '<pre>';
//        print_r($logs);exit;
        
        $this->render('index', array(
            'models' => $logs,
            'promoCodeID' => $promoCodeID,
            'group' => $group,
        ));
    }

}
